package com.website.system.controller;

import com.website.common.controller.BaseController;
import com.website.system.model.User;
import com.website.system.service.UserService;
import com.website.system.utils.GenerateRandNumber;
import com.website.system.utils.MailUtil;
import com.website.system.utils.SendVerificationCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("manage/register")
public class RegisterController extends BaseController {
    @Resource
    UserService userService;

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("/toRegisterUser")
    public String toregisterUser() {
        return "manage/registerUser";
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/RegisterUser",method = RequestMethod.POST)
    public String register(@ModelAttribute User user,String email){
        try {
            userService.insert(user);
            //调用MailUtil里面的发送邮箱的方法
            MailUtil.SendEmail(email);
            return SUCCESS_MSG;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR_MSG;
        }

    }
    /**
     * 发送验证码
     */
    @ResponseBody
    @RequestMapping(value="/sendMessage",method = RequestMethod.POST)
    public String sendMessage(String phoneNumber) {
        try {
            //生成六位随机验证码
            String verifyCode = GenerateRandNumber.getRandNumber();
            //调用发送短信接口
            //SendVerificationCode.getCodeMessage(phoneNumber,verifyCode);
            //返回数据,给页面的verificationCode赋值
            return verifyCode;

        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    /**
     * 注册成功通知
     */
    @ResponseBody
    @RequestMapping(value="/registerSuccess",method = RequestMethod.POST)
    public String registerSuccess(String phoneNumber) {
        try {
            //发送注册成功通知
            SendVerificationCode.registerSuccessMessage(phoneNumber);
            return null;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }


}
